﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shanda.Utility.Common
{
    public class PaginationHelper
    {

        public static string DoPagination(int pageIndex, int pageSize, int total, string link, Dictionary<string, string> parameters, bool compactMode)
        {
            int left = total % pageSize;
            int totalPages = total / pageSize;
            if (left > 0)
            {
                totalPages += 1;
            }
            if (totalPages == 0)
            {
                totalPages = 1;
            }

            StringBuilder sb = new StringBuilder("<ul style='float:right;' class='pagination'>");
            sb.Append(string.Format("<li class='totalRecords'>&nbsp;共{0}条记录&nbsp;</li>", total));
            sb.Append(string.Format("<li class='totalPages'>&nbsp;第{0}/{1}页&nbsp;</li>", pageIndex + 1, totalPages));
            if (totalPages == 1)
            {
                sb.Append(string.Format("<li class='currentPage'>&nbsp;{0}&nbsp;</li>", totalPages));
            }
            else
            {
                sb.Append(string.Format("<li class='firstPage'>&nbsp;<a href='{0}'>第一页</a>&nbsp;</li>", GetFinalLink(link, 0, pageSize, parameters, compactMode)));

                if (pageIndex > 0)
                {
                    sb.Append(string.Format("<li class='lastPage'>&nbsp;<a href='{0}'>上一页</a>&nbsp;</li>", GetFinalLink(link, pageIndex - 1, pageSize, parameters, compactMode)));
                }
                int startPage = pageIndex - 4;
                if (startPage < 1)
                {
                    startPage = 1;
                }
                int endPage = startPage + 9;
                if (endPage > totalPages)
                {
                    endPage = totalPages;
                }
                for (int i = startPage; i <= endPage; i++)
                {
                    if (i == pageIndex + 1)
                    {
                        sb.Append(string.Format("<li class='currentPage'>&nbsp;{0}&nbsp;</a></li>", i));
                    }
                    else
                    {
                        sb.Append(string.Format("<li class='indexPage'>&nbsp;<a href='{0}'>[{1}]</a>&nbsp;</li>", GetFinalLink(link, i - 1, pageSize, parameters, compactMode), i));
                    }
                }
                if (pageIndex < totalPages - 1)
                {
                    sb.Append(string.Format("<li class='nextPage'>&nbsp;<a href='{0}'>下一页</a>&nbsp;</li>", GetFinalLink(link, pageIndex + 1, pageSize, parameters, compactMode)));
                    sb.Append(string.Format("<li class='nextPage'>&nbsp;<a href='{0}'>最后页</a>&nbsp;</li>", GetFinalLink(link, totalPages - 1, pageSize, parameters, compactMode)));

                }

            }
            sb.Append("</ul>");

            return sb.ToString();
        }

        private static string GetFinalLink(string link, int pageIndex, int pageSize, Dictionary<string, string> otherParameters,bool compactMode)
        {
            string flink = link;
            if (compactMode && !flink.Contains('?'))
            {
                if (!flink.EndsWith("/"))
                {
                    flink = flink + "/"; ;
                }
                flink = flink + pageIndex + "/" + pageSize + "?";

            }
            else
            {
                if (flink.Contains('?'))
                {
                    flink = flink + "&PageIndex=" + pageIndex + "&PageSize=" + pageSize + "&";
                }
                else
                {
                    flink = flink + "?PageIndex=" + pageIndex + "&PageSize=" + pageSize + "&";
                }
            }
            if (otherParameters != null && otherParameters.Count > 0)
            {
                foreach (string key in otherParameters.Keys)
                {
                    flink = flink + key + "=" + otherParameters[key] + "&";
                }
                flink = flink.Remove(flink.Length - 1);
            }
            return flink;
        }

        public static string DoPagination2( int pageIndex, int pageSize, int total, string link, Dictionary<string, string> parameters, bool compactMode )
        {
            int left = total % pageSize;
            int totalPages = total / pageSize;
            if (left > 0)
            {
                totalPages += 1;
            }
            if (totalPages == 0)
            {
                totalPages = 1;
            }

            StringBuilder sb = new StringBuilder( "<ul style='float:right;' class='pagination'>" );
            sb.Append( string.Format( "<li class='totalRecords'>&nbsp;共{0}条记录&nbsp;</li>", total ) );
            sb.Append( string.Format( "<li class='totalPages'>&nbsp;第{0}/{1}页&nbsp;</li>", pageIndex + 1, totalPages ) );
            if (totalPages == 1)
            {
                sb.Append( string.Format( "<li class='currentPage'>&nbsp;{0}&nbsp;</li>", totalPages ) );
            }
            else
            {
                sb.Append( string.Format( "<li class='firstPage'>&nbsp;<a href='{0}'>第一页</a>&nbsp;</li>", GetFinalLink2( link, 0, pageSize, parameters, compactMode ) ) );

                if (pageIndex > 0)
                {
                    sb.Append( string.Format( "<li class='lastPage'>&nbsp;<a href='{0}'>上一页</a>&nbsp;</li>", GetFinalLink2( link, pageIndex - 1, pageSize, parameters, compactMode ) ) );
                }
                int startPage = pageIndex - 4;
                if (startPage < 1)
                {
                    startPage = 1;
                }
                int endPage = startPage + 9;
                if (endPage > totalPages)
                {
                    endPage = totalPages;
                }
                for (int i = startPage; i <= endPage; i++)
                {
                    if (i == pageIndex + 1)
                    {
                        sb.Append( string.Format( "<li class='currentPage'>&nbsp;{0}&nbsp;</a></li>", i ) );
                    }
                    else
                    {
                        sb.Append( string.Format( "<li class='indexPage'>&nbsp;<a href='{0}'>[{1}]</a>&nbsp;</li>", GetFinalLink2( link, i - 1, pageSize, parameters, compactMode ), i ) );
                    }
                }
                if (pageIndex < totalPages - 1)
                {
                    sb.Append( string.Format( "<li class='nextPage'>&nbsp;<a href='{0}'>下一页</a>&nbsp;</li>", GetFinalLink2( link, pageIndex + 1, pageSize, parameters, compactMode ) ) );
                    sb.Append( string.Format( "<li class='nextPage'>&nbsp;<a href='{0}'>最后页</a>&nbsp;</li>", GetFinalLink2( link, totalPages - 1, pageSize, parameters, compactMode ) ) );

                }

            }
            sb.Append( "</ul>" );

            return sb.ToString();
        }

        private static string GetFinalLink2( string link, int pageIndex, int pageSize, Dictionary<string, string> otherParameters, bool compactMode )
        {
            string flink = link;
            if (compactMode && !flink.Contains( '?' ))
            {
                if (!flink.EndsWith( "/" ))
                {
                    flink = flink + "/"; ;
                }
                flink = flink + pageIndex + "/" + pageSize + "?";

            }
            else
            {
                if (flink.Contains( '?' ))
                {
                    flink = flink + "&rPageIndex=" + pageIndex + "&rPageSize=" + pageSize + "&";
                }
                else
                {
                    flink = flink + "?rPageIndex=" + pageIndex + "&rPageSize=" + pageSize + "&";
                }
            }
            if (otherParameters != null && otherParameters.Count > 0)
            {
                foreach (string key in otherParameters.Keys)
                {
                    flink = flink + key + "=" + otherParameters[key] + "&";
                }
                flink = flink.Remove( flink.Length - 1 );
            }
            return flink;
        }

        public static string DoAjaxPagination(int pageIndex, int pageSize, int total, string jsFunctionName)
        {
            int left = total % pageSize;
            int totalPages = total / pageSize;
            if (left > 0)
            {
                totalPages += 1;
            }
            if (totalPages == 0)
            {
                totalPages = 1;
            }

            StringBuilder sb = new StringBuilder("<ul style='float:right;' class='pagination'>");
            sb.Append(string.Format("<li class='totalRecords'>&nbsp;共{0}条记录&nbsp;</li>", total));
            sb.Append(string.Format("<li class='totalPages'>&nbsp;第{0}/{1}页&nbsp;</li>", pageIndex + 1, totalPages));
            if (totalPages == 1)
            {
                sb.Append(string.Format("<li class='currentPage'>&nbsp;{0}&nbsp;</li>", totalPages));
            }
            else
            {
                sb.Append(string.Format("<li class='firstPage'>&nbsp;<a href='javascript:void(0)' onclick='javascript:" + jsFunctionName + "({0})'>第一页</a>&nbsp;</li>", "0"));

                if (pageIndex > 0)
                {
                    sb.Append(string.Format("<li class='lastPage'>&nbsp;<a href='javascript:void(0)' onclick='javascript:" + jsFunctionName + "({0})'>上一页</a>&nbsp;</li>", (pageIndex - 1).ToString()));
                }
                int startPage = pageIndex - 4;
                if (startPage < 1)
                {
                    startPage = 1;
                }
                int endPage = startPage + 9;
                if (endPage > totalPages)
                {
                    endPage = totalPages;
                }
                for (int i = startPage; i <= endPage; i++)
                {
                    if (i == pageIndex + 1)
                    {
                        sb.Append(string.Format("<li class='currentPage'>&nbsp;{0}&nbsp;</a></li>", i));
                    }
                    else
                    {
                        sb.Append(string.Format("<li class='indexPage'>&nbsp;<a href='javascript:void(0)' onclick='javascript:" + jsFunctionName + "({0})'>[{1}]</a>&nbsp;</li>",  (i - 1).ToString(), i.ToString()));
                    }
                }
                if (pageIndex < totalPages - 1)
                {
                    sb.Append(string.Format("<li class='nextPage'>&nbsp;<a href='javascript:void(0)' onclick='javascript:" + jsFunctionName + "({0})'>下一页</a>&nbsp;</li>", pageIndex + 1));
                    sb.Append(string.Format("<li class='nextPage'>&nbsp;<a href='javascript:void(0)' onclick='javascript:" + jsFunctionName + "({0})'>最后页</a>&nbsp;</li>", totalPages - 1));

                }

            }
            sb.Append("</ul>");

            return sb.ToString();
        }


    }
}
